package com.maxsalad.uc.dao;

import com.maxsalad.common.dao.BaseDao;
import com.maxsalad.uc.dto.DPlatformShopDto;
import com.maxsalad.uc.pojo.TPlatformShopEntity;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-16.
 */
@Repository
public class PlatformShopDao extends BaseDao<TPlatformShopEntity> {
    private void baseSearchParam(TPlatformShopEntity platformShopEntity, StringBuffer hql) {
        if (platformShopEntity == null) {
            return;
        }
        if (platformShopEntity.getId() != 0) {
            hql.append(" and t.id =:id");
        }

        //平台ID
        if (platformShopEntity.getPlatformId() > 0) {
            hql.append(" and t.platformId =:platformId");
        }
        //店铺ID
        if (platformShopEntity.getShopId() > 0) {
            hql.append(" and t.shopId =:shopId");
        }
        //平台店铺ID
        if (StringUtils.isNotEmpty(platformShopEntity.getShopPid())) {
            hql.append(" and t.shopPid =:shopPid");
        }
        //baseUrl
        if (StringUtils.isNotEmpty(platformShopEntity.getBaseUrl())) {
            hql.append(" and t.baseUrl like :baseUrl");
            platformShopEntity.setBaseUrl("%" + platformShopEntity.getBaseUrl() + "%");
        }
        //appKey
        if (StringUtils.isNotEmpty(platformShopEntity.getAppKey())) {
            hql.append(" and t.appKey like :appKey");
            platformShopEntity.setAppKey("%" + platformShopEntity.getAppKey() + "%");
        }
        //appSecret
        if (StringUtils.isNotEmpty(platformShopEntity.getAppSecret())) {
            hql.append(" and t.appSecret like :appSecret");
            platformShopEntity.setAppSecret("%" + platformShopEntity.getAppSecret() + "%");
        }

        //description
        if (StringUtils.isNotEmpty(platformShopEntity.getDescription())) {
            hql.append(" and t.description like :description");
            platformShopEntity.setDescription("%" + platformShopEntity.getDescription() + "%");
        }

    }

    private List<DPlatformShopDto> parseMap(Map map) {
        List<Object[]> resultList = (List<Object[]>) map.get("rows");
        if (resultList != null && resultList.size() > 0) {
            List<DPlatformShopDto> list = new ArrayList<DPlatformShopDto>();
            DPlatformShopDto dto = null;
            for (Object[] item : resultList) {
                dto = new DPlatformShopDto();
                dto.setId(Long.valueOf(item[0].toString()));
                dto.setPlatformId(Long.valueOf(item[1].toString()));
                dto.setShopId(Long.valueOf(item[2].toString()));
                dto.setShopPid(item[3].toString());
                dto.setBaseUrl(item[4].toString());
                dto.setAppKey(item[5].toString());
                dto.setAppSecret(item[6].toString());
                dto.setDescription(item[7].toString());
                dto.setCreateUser(item[8].toString());
                dto.setCreateTime(Long.valueOf(item[9].toString()));
                dto.setUpdateUser(item[10].toString());
                dto.setUpdateTime(Long.valueOf(item[11].toString()));
                dto.setPlatformCode(item[12].toString());
                dto.setPlatformName(item[13].toString());
                dto.setShopCode(item[14].toString());
                dto.setShopName(item[15].toString());

                list.add(dto);
            }

            return list;
        }

        return null;
    }

    /**
     * 查询所有平台信息
     *
     * @param platformShopEntity
     * @return
     * @throws Exception
     */
    public Map queryAllPlatformShop(TPlatformShopEntity platformShopEntity) throws Exception {
        StringBuffer hql = new StringBuffer("select t.id, t.platformId,t.shopId,t.shopPid,t.baseUrl,t.appKey,t.appSecret,t.description,t.createUser,t.createTime,t.updateUser,t.updateTime,");
        hql.append("tPlatform.platformCode,tPlatform.platformName,tShop.shopCode,tShop.shopName ");
        hql.append(" from TPlatformShopEntity t,TPlatformEntity tPlatform,TShopEntity tShop ");
        hql.append(" where 1=1 and t.platformId=tPlatform.id and t.shopId=tShop.id ");
        this.baseSearchParam(platformShopEntity, hql);
        hql.append(" order by t.createTime desc ");
        Map resultMap = this.pageQueryByHql(hql.toString(), platformShopEntity);
        if (MapUtils.getLong(resultMap, "total", 0L) > 0) {
            List<DPlatformShopDto> list = parseMap(resultMap);
            resultMap.put("rows", list);
        }

        return resultMap;
    }

    /**
     * 查询所有店铺信息(不分页)
     *
     * @param platformShopEntity
     * @return
     * @throws Exception
     */
    public List<DPlatformShopDto> queryAllPlatformShopNoPage(TPlatformShopEntity platformShopEntity) throws Exception {
        StringBuffer hql = new StringBuffer("select t.id, t.platformId,t.shopId,t.shopPid,t.baseUrl,t.appKey,t.appSecret,t.description,t.createUser,t.createTime,t.updateUser,t.updateTime,");
        hql.append("tPlatform.platformCode,tPlatform.platformName,tShop.shopCode,tShop.shopName ");
        hql.append(" from TPlatformShopEntity t,TPlatformEntity tPlatform,TShopEntity tShop ");
        hql.append(" where 1=1 and t.platformId=tPlatform.id and t.shopId=tShop.id ");
        this.baseSearchParam(platformShopEntity, hql);
        hql.append(" order by t.createTime desc ");
        return this.findByHql(hql.toString(), platformShopEntity);
    }
}
